package bitmap;

public class BMPobj
{
	public int[][] image;
	int rows;
	int cols;

	public BMPobj(int[][] image)
	{
		this.image = image;
		this.rows = image.length;
		this.cols = image[0].length;
	}

	public BMPobj(int rows, int cols)
	{
		this.rows = rows;
		this.cols = cols;
		this.image = new int[rows][cols];
	}

	public void fill(Color color)
	{
		int colorInt = color.getRgbInt();
		for (int r = 0; r < rows; r++)
		{
			int[] a = image[r];
			for (int c = 0; c < cols; c++)
			{
				a[c] = colorInt;
			}
		}
	}

	public void fill(int color)
	{
		for (int r = 0; r < rows; r++)
		{
			int[] a = image[r];
			for (int c = 0; c < cols; c++)
			{
				a[c] = color;
			}
		}
	}

	public void point(int x, int y, Color c, boolean blend)
	{
		
		if (x >= 0 && x < rows && y >= 0 && y < cols)
		{
			Color res = c;
			if (blend)
			{
				Color bkg = new Color(image[x][y]);
				res = Color.addColors(bkg, c);
			}

			// System.out.println(x + "," + y);
			image[x][y] = res.getRgbInt();
		}

	}

	public void point(int x, int y, int c, boolean blend)
	{
		
		if (x >= 0 && x < rows && y >= 0 && y < cols)
		{
			int res = c;
			if (blend)
			{
				Color bkg = new Color(image[x][y]);
				res = Color.addColors(bkg, new Color(c)).getRgbInt();
			}
			// System.out.println(x + "," + y);
			image[x][y] = res;
		}
	}

	public void point(int x, int y, Color c)
	{
		point(x, y, c, false);
	}

	public void point(int x, int y, int c)
	{
		point(x, y, c, false);
	}

	public void drawCircle(int cx, int cy, int r, Color c)
	{
		int color = c.getRgbInt();
		for (int x = -r; x < r; x++)
		{
			int vv = (int) Math.sqrt(r * r - x * x);
			int y = (int) (cy - vv);
			point(cx + x, y, color);
			y = (int) (cy + vv);
			point(cx + x, y, color);
		}

		for (int y = -r; y < r; y++)
		{
			int vv = (int) Math.sqrt(r * r - y * y);
			int x = (int) (cx - vv);
			point(x, cy + y, color);
			x = (int) (cx + vv);
			point(x, cy + y, color);
		}
	}

	public void fillCircle(int cx, int cy, int r, Color fill, Color border)
	{
		fillCircle(cx, cy, r, fill, border, false);
	}
	public void fillCircle(int cx, int cy, int r, Color fill, Color border, boolean blend)
	{
		int f = fill.getRgbInt();
		int r2 = r * r;

		for (int x = -r; x <= r; x++)
		{
			for (int y = -r; y <= r; y++)
			{
				//System.out.println(x + "," + y);
				if ((x * x + y * y) <= r2)
				{
					// System.out.println(x+","+y);
					point(cx + x, cy + y, f, blend);
				}
			}
		}

		if (border != null)
		{
			int b = border.getRgbInt();
			drawCircle(cx, cy, r, new Color(b));
		}
	}

	public void drawLine(int x1, int y1, int x2, int y2, Color c)
	{
		int xSpan;
		int xStart;
		int ySpan;
		int yStart;

		if (y1 > y2)
		{
			ySpan = y1 - y2;
			yStart = y2;
		}
		else
		{
			ySpan = y2 - y1;
			yStart = y1;
		}

		if (x2 == x1)
		{
			for (int y = 0; y <= ySpan; y++)
			{
				point(x1, y + yStart, c);
			}
		}
		else
		{
			double a = (y2 - y1) / (x2 - x1);
			double b = y1 - a * x1;
			if (x1 > x2)
			{
				xSpan = x1 - x2;
				xStart = x2;
			}
			else
			{
				xSpan = x2 - x1;
				xStart = x1;
			}

			for (int x = 0; x < xSpan; x++)
			{
				int xx = x + xStart;
				int y = (int) (b + (xx) * a);
				point(xx, y, c);
			}

			for (int y = 0; y < ySpan; y++)
			{
				int yy = y + yStart;
				int x = (int) (((yy) - b) / a);
				point(x, yy, c);
			}
		}
	}

	public void drawSquare(int x1, int y1, int x2, int y2, Color c)
	{
		drawLine(x1, y1, x2, y1, c);
		drawLine(x2, y1, x2, y2, c);
		drawLine(x2, y2, x1, y2, c);
		drawLine(x1, y2, x1, y1, c);
	}

	public void fillSquare(int x1, int y1, int x2, int y2, Color fill, Color border)
	{
		fillSquare(x1, y1, x2, y2, fill, border, false);
	}

	public void fillSquare(int x1, int y1, int x2, int y2, Color fill, Color border, boolean blend)
	{

		int xStart, xStop, yStart, yStop;

		if (y1 > y2)
		{
			yStop = y1;
			yStart = y2;
		}
		else
		{
			yStop = y2;
			yStart = y1;
		}

		if (x1 > x2)
		{
			xStop = x1;
			xStart = x2;
		}
		else
		{
			xStop = x2;
			xStart = x1;
		}
		int colorFill = fill.getRgbInt();
		for (int x = xStart; x < xStop; x++)
		{
			for (int y = yStart; y < yStop; y++)
			{
				point(x, y, colorFill, blend);
			}

		}
		if (border != null)
		{
			drawSquare(x1, y1, x2, y2, border);
		}
	}
	
	public void drawOval(int x1, int y1, int x2, int y2, Color c)
	{
		
	}

}
